package org.esprit.ocm.client.model.ec2.datasource;

import java.util.ArrayList;
import java.util.List;

import org.esprit.ocm.client.model.ec2.KeyPairRecord;
import org.esprit.ocm.dto.ec2.impl.KeyPairInfoMappingDto;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class KeyPairLocalDS extends DataSource {

	private static KeyPairLocalDS	instance	= null;

	public static KeyPairLocalDS getInstance(List<KeyPairInfoMappingDto> keys) {
//		if (instance == null) {
			instance = new KeyPairLocalDS("keyPairLocalDS", keys);
//		}
		return instance;
	}

	public KeyPairLocalDS(String id, List<KeyPairInfoMappingDto> keys) {

//		setID(id);

		DataSourceTextField keyNameField = new DataSourceTextField("keyName", "keyName");
		DataSourceTextField keyFingerprintField = new DataSourceTextField("keyFingerprint", "keyFingerprint");
		DataSourceTextField keyMaterialField = new DataSourceTextField("keyMaterial", "keyMaterial");

		setFields(keyNameField, keyFingerprintField, keyMaterialField);

		List<ListGridRecord> l = new ArrayList<ListGridRecord>();
		for (KeyPairInfoMappingDto from : keys) {

			KeyPairRecord to = new KeyPairRecord();
			
			to.setKeyName(from.getKeyName());
			to.setKeyFingerprint(from.getKeyFingerprint());
			to.setKeyMaterial(from.getKeyMaterial());
			
			l.add(to);
		}

		// setTestData((DataClass[]) imageRecords.toArray());
		setTestData(l.toArray(new KeyPairRecord[0]));
		setClientOnly(true);
	}

}
